package io.renren.modules.generator.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.generator.entity.ItemEntity;
import io.renren.modules.generator.enums.HdTypeEnum;
import io.renren.modules.generator.service.ItemService;
import io.renren.modules.generator.service.SpecService;
import io.renren.modules.generator.vo.ItemSelectVo;
import io.renren.modules.generator.vo.ItemVo;
import io.renren.modules.generator.vo.SpecVo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 物料信息
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-12-11 00:13:08
 */
@RestController
@RequestMapping("generator/item")
public class ItemController {
    @Autowired
    private ItemService itemService;

    @Autowired
    SpecService specService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:item:list")
    public R list(@RequestParam Map<String, Object> params){
        if (params.containsKey("key") && StringUtils.isNotBlank(params.get("key").toString())) {
            params.put("name", params.get("key"));
        }
        PageUtils page = itemService.queryPage(params);
        List<SpecVo> specs = specService.listForSelect();
        Map<Long, String> specMap = specs.stream().collect(Collectors.toMap(SpecVo::getValue, SpecVo::getLabel));
        List<ItemEntity> entitys = (List<ItemEntity>) page.getList();
        List<ItemVo> reponse = new ArrayList<>();
        entitys.stream().forEach(x->{
            ItemVo vo = new ItemVo();
            BeanUtils.copyProperties(x, vo);
            vo.setSpecStr(specMap.get(vo.getSpecId()));
            vo.setHdTypeStr(HdTypeEnum.getString(vo.getHdType().intValue()));
            reponse.add(vo);
        });
        page.setList(reponse);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/listForSelect")
    @RequiresPermissions("generator:item:list")
    public R listForSelect(){
        List<ItemSelectVo> response = new ArrayList<>();
        List<ItemEntity> page = itemService.selectList(Condition.empty());
        page.stream().forEach(x->{
            ItemSelectVo vo = new ItemSelectVo();
            vo.setValue(x.getGuid());
            vo.setLabel(x.getName());
            vo.setSpecId(x.getSpecId());
            response.add(vo);
        });
        return R.ok().put("data", response);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:item:info")
    public R info(@PathVariable("id") Long id){
			ItemEntity item = itemService.selectById(id);

        return R.ok().put("item", item);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:item:save")
    public R save(@RequestBody ItemEntity item){
        item.setCreateTime(new Date());
        item.setCreateUser(ShiroUtils.getUserEntity().getUsername());
        item.setUpdateTime(item.getCreateTime());
        item.setUpdateUser(item.getCreateUser());
        itemService.insert(item);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:item:update")
    public R update(@RequestBody ItemEntity item){

        item.setCreateUser(null);
        item.setCreateTime(null);
        item.setUpdateTime(new Date());
        item.setUpdateUser(ShiroUtils.getUserEntity().getUsername());
        itemService.updateById(item);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:item:delete")
    public R delete(@RequestBody Long[] ids){
			itemService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
